home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / wt_jan92.zip / DPMI.ZIP / NBCHAT.PAS < prev    next >
Pascal/Delphi Source File  |  1991-12-23  |  6KB  |  221 lines

  1. NBCHAT.PAS   - Example Windows/NetBIOS Chat program.
  2. {*********************************************************}
  3. {*                  NBCHAT.PAS 1.00                      *}
  4. {*        Copyright (c) TurboPower Software 1991.        *}
  5. {*                 All rights reserved.                  *}
  6. {*********************************************************}
  7. {$S-,R-,V-}
  8. program NBChat;
  9.   {-A simple NetBIOS chat program for Windows}
  10.  
  11. uses WinProcs, WinCrt, WinDPMI, TNetBIOS, UNetBIOS;
  12.  
  13. const
  14.   Pending : Boolean = False;
  15.   Msg : String = '';
  16.   NameAdded : Boolean = False;
  17.   RealMemAllocated : Boolean = False;
  18.   SendMemAllocated : Boolean = False;
  19.   RecNCBAllocated : Boolean = False;
  20.   SendNCBAllocated : Boolean = False;
  21.   PostAllocated : Boolean = False;
  22.   Exiting : Boolean = False;
  23.  
  24. type
  25.   StringPtr = ^String;
  26.  
  27. var
  28.   SendNBName : NBNameStr;
  29.   RecNBName  : NBNameStr;
  30.   NBNameNo : Byte;
  31.   Ret : Byte;
  32.   LSN : Byte;
  33.   SR, SP, SendSP, SendSR : StringPtr;
  34.   Regs : DPMIRegisters;
  35.   RecN, SendN : WinNcb;
  36.   Post : WindowsPostType;
  37.   SendPost : WindowsPostType;
  38.   SaveExitProc : Pointer;
  39.  
  40.   function Num2Str(Num : Byte) : String;
  41.   var
  42.     S : String;
  43.   begin
  44.     Str(Num, S);
  45.     Num2Str := S;
  46.   end;
  47.  
  48.   function Pad(S : String; Num : Byte) : String;
  49.   var
  50.     Len : Byte;
  51.   begin
  52.     Len := Length(S);
  53.     if Len < Num then
  54.       FillChar(S[Succ(Len)], Num-Len, ' ');
  55.     S[0] := Chr(Num);
  56.     Pad := S;
  57.   end;
  58.  
  59.   function GetRealModeMem(Size : LongInt;
  60.                           var RealPtr : StringPtr;
  61.                           var ProtectedPtr : StringPtr) : Boolean;
  62.     {-Gets real mode addressable memory (paragraph aligned) and returns both a
  63.       pointer for real and protected mode. Returns True if successful. Size is
  64.       the number of bytes to allocate.}
  65.   var
  66.     L : LongInt;
  67.   begin
  68.     L := GlobalDosAlloc(Size);
  69.     if L > 0 then begin
  70.       RealPtr := Ptr(DoubleWord(L).HiWord, 0);
  71.       ProtectedPtr := Ptr(DoubleWord(L).LoWord, 0);
  72.       GetRealModeMem := True;
  73.     end
  74.     else
  75.       GetRealModeMem := False;
  76.   end;
  77.  
  78.   procedure FreeRealModeMem(ProtectedPtr : StringPtr);
  79.     {-Free memory previously allocated with GetRealModeMem.}
  80.   begin
  81.     GlobalDosFree(SegOfs(ProtectedPtr).Segm);
  82.   end;
  83.  
  84.   procedure PostRoutine(LastError : Byte; N : WinNCBPtr); Far;
  85.   begin
  86.     if Exiting then Exit;
  87.     Pending := True;
  88.     if LastError = 0 then
  89.       Msg := SP^
  90.     else
  91.       Msg := 'NetBIOS error = ' + Num2Str(LastError);
  92.     ReceiveDatagram(N^, NBNameNo, False, Post, SizeOf(String), SR);
  93.   end;
  94.  
  95.   procedure GetNames;
  96.   begin
  97.     Write('Enter name for this station: ');
  98.     ReadLn(SendNBName);
  99.     if Length(SendNBName) = 0 then
  100.       Halt;
  101.     SendNBName := Pad(SendNBName, SizeOf(SendNBName) - 1);
  102.     Write('Enter name for partner: ');
  103.     ReadLn(RecNBName);
  104.     if Length(RecNBName) = 0 then
  105.       Halt;
  106.     RecNBName := Pad(RecNBName, SizeOf(RecNBName) - 1);
  107.   end;
  108.  
  109.   procedure ShowIncoming;
  110.   begin
  111.     WriteLn('<incoming>', Msg);
  112.     Pending := False;
  113.   end;
  114.  
  115.   procedure SendOutgoing;
  116.   begin
  117.     Write('Enter outgoing message: ');
  118.     ReadLn(SendSP^);
  119.     if Length(SendSP^) <> 0 then
  120.       SendDatagram(SendN, NBNameNo, RecNBName, False, SendPost, SizeOf(String), SendSR);
  121.   end;
  122.  
  123.   procedure MessageLoop;
  124.   var
  125.     C : Char;
  126.   begin
  127.     WriteLn('Press space bar to enter message, ESC to quit');
  128.     ReceiveDatagram(RecN, NBNameNo, False, Post, SizeOf(String), SR);
  129.     repeat
  130.       while not KeyPressed do begin
  131.         if Pending then
  132.           ShowIncoming;
  133.       end;
  134.       C := ReadKey;
  135.       if C <> ^[ then
  136.         SendOutgoing;
  137.     until C = ^[;
  138.   end;
  139.  
  140.   procedure AllocateMemory;
  141.   begin
  142.     if GetRealModeMem(SizeOf(String), SR, SP) then
  143.       RealMemAllocated := True
  144.     else begin
  145.       WriteLn('Unable to obtain real mode memory for messages');
  146.       Halt;
  147.     end;
  148.     if GetRealModeMem(SizeOf(String), SendSR, SendSP) then
  149.       SendMemAllocated := True
  150.     else begin
  151.       WriteLn('Unable to obtain real mode memory for messages');
  152.       Halt;
  153.     end;
  154.     if AllocateWinNCB(RecN) then
  155.       RecNCBAllocated := True
  156.     else begin
  157.       WriteLn('Unable to allocate NCBs');
  158.       Halt;
  159.     end;
  160.     if AllocateWinNCB(SendN) then
  161.       SendNCBAllocated := True
  162.     else begin
  163.       WriteLn('Unable to allocate NCBs');
  164.       Halt;
  165.     end;
  166.     FillChar(SendPost, SizeOf(SendPost), 0);
  167.     if GetWindowsPostRoutine(PostRoutine, DSeg, Post) then
  168.       PostAllocated := True
  169.     else begin
  170.       WriteLn('Unable to obtain real mode callback address');
  171.       Exit;
  172.     end;
  173.   end;
  174.  
  175.   procedure NBExitProc; Far;
  176.   var
  177.     Ret : Byte;
  178.   begin
  179.     ExitProc := SaveExitProc;
  180.     Exiting := True;
  181.     if RecNCBAllocated then begin
  182.       Ret := CancelRequest(RecN);
  183.       FreeWinNCB(RecN);
  184.     end;
  185.     if SendNCBAllocated then begin
  186.       Ret := CancelRequest(SendN);
  187.       FreeWinNCB(SendN);
  188.     end;
  189.     if NameAdded then
  190.       Ret := NetBIOSDeleteName(SendNBName);
  191.     if RealMemAllocated then
  192.       FreeRealModeMem(SP);
  193.     if SendMemAllocated then
  194.       FreeRealModeMem(SendSP);
  195.     if PostAllocated then
  196.       FreeWindowsPostRoutine(Post);
  197.   end;
  198.  
  199. begin
  200.   if InRealMode then begin
  201.     WriteLn('This program is not compatible with Real Mode.');
  202.     Halt;
  203.   end;
  204.   if not NetBIOSInstalled then begin
  205.     WriteLn('NetBIOS not installed');
  206.     Halt;
  207.   end;
  208.   GetNames;
  209.   SaveExitProc := ExitProc;
  210.   ExitProc := @NBExitProc;
  211.   Ret := NetBIOSAddName(SendNBName, NBNameNo);
  212.   if Ret = 0 then
  213.     NameAdded := True
  214.   else begin
  215.     WriteLn('Error adding NetBIOS name');
  216.     Halt;
  217.   end;
  218.   AllocateMemory;
  219.   MessageLoop;
  220. end.
  221.